package ss.pku.meeting.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import ss.pku.meeting.entities.Msg;
import ss.pku.meeting.service.DepartmentService;
import ss.pku.meeting.service.MessageService;
import ss.pku.meeting.util.TokenUtil;

import java.util.HashMap;
import java.util.Map;

@RestController
public class MessageController {

    @Autowired
    MessageService messageService;

    @Autowired
    DepartmentService departmentService;

    @GetMapping("/message/unreadCount")
    public Msg getUnreadMessageCount(@RequestHeader Map<String,Object> header) {
        String token = (String) header.get("authorization");
        String deptNo = TokenUtil.getNo(token);
        int deptId = departmentService.seletByNo(deptNo);
        Map<String ,Object> map = new HashMap<>();
        Integer count = messageService.getUnreadCount(deptId);
        map.put("count",count);

        return Msg.success().add(map);
    }

    @GetMapping("/message/history")
    public Msg history(@RequestParam Map<String,Object> para, @RequestHeader Map<String,Object> header){
        String token = (String) header.get("authorization");
        String deptNo = TokenUtil.getNo(token);
        int deptId = departmentService.seletByNo(deptNo);
        para.put("deptId",deptId);
        return messageService.select(para);
    }

    @PutMapping("/message/read")
    public Msg read(@RequestBody Map<String,Object> para) {
        Integer id = (Integer) para.get("messageId");
        return messageService.update(id);
    }
}
